<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">	
	
	<xsl:include href="../main.xsl" />
	<xsl:include href="../menu.xsl" />
	<xsl:include href="../common/navigation.xsl" />

	<xsl:template match="/data">
		<html>
		
		<head>
			<title>
				<xsl:value-of select="$pageTitle" />
			</title>
			<xsl:call-template name="meta-head" />
			<script type="text/javascript" src="{$baseURI}/js/mail.js" />
			<link rel="stylesheet" href="{$baseURI}/css/mail.css" type="text/css" media="all" />
		</head>
		<body>
			
			<xsl:call-template name="top-userbar" />

			<div class="page-header">
				<xsl:call-template name="logo" />

				<xsl:call-template name="menu" />
			</div>

			<div class="page-content-wrapper">
				<div class="page-blocks">
					<xsl:call-template name="default-blocks-top" />
				</div>
	
				<div class="page-content">
					<xsl:apply-templates select="response" />
				</div>
			</div>

			<xsl:call-template name="footer" />
		</body>
		</html>
	</xsl:template>
	
	
	<xsl:template match="response">
		<xsl:param name="cnt" select="count(object)" />
		
		<h1>Почта</h1>
		
		<div class="mail-bar">
			<div class="controls">
				<span class="g-button">
					<a href="{$baseURI}/mail/write">Написать другу</a>
				</span>
			</div>
			<div class="label">
				<xsl:text>Показано </xsl:text>
				<xsl:value-of select="$cnt" />
				<xsl:text> из </xsl:text>
				<xsl:value-of select="pages/total" />
				<xsl:text> сообщений</xsl:text>
			</div>
		</div>
		
		<xsl:if test="$cnt = 0">
			<div class="simple_message">Писем нет</div>
		</xsl:if>
		
		<xsl:if test="$cnt != 0">
			<form action="{$baseURI}/mail/delete" method="post" id="form_delete_mail">		
				<table class="mail-list">
					<thead>
						<tr>
							<th>
								<input type="checkbox" onchange="selectAllRows(this);" name="goodcheckbox" value="" />
							</th>
							<th />
							<th>тема</th>
							<th class="date">последнее сообщение</th>
						</tr>
					</thead>
					<tbody>
						<xsl:apply-templates select="object" />
					</tbody>
				</table>
				<p>
					<input onclick="deleteFormConfirm(); return false;" type="submit" value="Удалить выбранные" />
				</p>
			</form>
		</xsl:if>
		
		<xsl:apply-templates select="pages" mode="pages-navigation">
			<xsl:with-param name="href">
				<xsl:value-of select="$baseURI" />
				<xsl:text>/mail</xsl:text>
			</xsl:with-param>
		</xsl:apply-templates>
	</xsl:template>
	
	<xsl:template match="object[@type='message']">
		<xsl:param name="senderLogin" select="property[@name='sender_login']/value" />
		<xsl:param name="addressedLogin" select="property[@name='addressed_login']/value" />
		
		<tr>
			<xsl:if test="@read = '0'">
				<xsl:attribute name="class">new</xsl:attribute>
			</xsl:if>
			<td class="delete-message">
				<input class="delcheck" type="checkbox" name="delete[{@type-id}][]" value="{@id}" onchange="$(this).parent().parent().toggleClass('selected');" />
			</td>
			<td class="sender">
				<xsl:choose>
					<xsl:when test="property[@name='sender_id']/value = $userId">
						<xsl:text>Я → </xsl:text>
						<a href="{$baseURI}/user/{$addressedLogin}">
							<xsl:value-of select="$addressedLogin" />
						</a>
					</xsl:when>
					<xsl:otherwise>
						<a href="{$baseURI}/user/{$senderLogin}">
							<xsl:value-of select="$senderLogin" />
						</a>
						<xsl:text> → я</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</td>
			<td class="subject">
				<a href="{$baseURI}/mail/read/{@type}/{@id}">
					<xsl:value-of select="property[@name='subject']/value" />
				</a>
				<xsl:if test="number(property[@name='count_comments']/value) &gt; 0">
					<xsl:text disable-output-escaping="yes">&amp;nbsp;&amp;nbsp;&amp;nbsp;</xsl:text>
					<xsl:value-of select="property[@name='count_comments']/value" />
				</xsl:if>
			</td>
			<td class="date">
				<xsl:value-of select="property[@name='date']/value" />
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="object[@type='system_notification']">
		<tr>
			<xsl:if test="@read = '0'">
				<xsl:attribute name="class">new</xsl:attribute>
			</xsl:if>
			<td class="delete-message">
				<input class="delcheck" type="checkbox" name="delete[{@type-id}][]" value="{@id}" onchange="$(this).parent().parent().toggleClass('selected');" />
			</td>
			<td class="sender">
				<xsl:text>уведомление</xsl:text>
			</td>
			<td class="subject">
				<a href="{$baseURI}/mail/read/{@type}/{@id}">
					<xsl:value-of select="property[@name='subject']/value" />
				</a>
			</td>
			<td class="date">
				<xsl:value-of select="property[@name='date']/value" />
			</td>
		</tr>
	</xsl:template>
	
</xsl:stylesheet>